package org.micode.libs.smon;

import org.micode.libs.smon.utils.CpuUtils;
import org.micode.libs.smon.utils.DiskUtils;
import org.micode.libs.smon.utils.JvmUtils;
import org.micode.libs.smon.utils.MemoryUtils;
import org.micode.libs.smon.utils.SystemUtils;
import org.micode.libs.smon.vo.CpuInfo;
import org.micode.libs.smon.vo.DiskInfo;
import org.micode.libs.smon.vo.JavaInfo;
import org.micode.libs.smon.vo.JvmInfo;
import org.micode.libs.smon.vo.JvmRuntimeInfo;
import org.micode.libs.smon.vo.LocalInfo;
import org.micode.libs.smon.vo.MemoryInfo;

import java.util.List;

/**
 * 系统监控器
 */
public class SystemMonitor {

    private CpuInfo cpuInfo;

    private List<DiskInfo> diskInfoList;

    private JavaInfo javaInfo;

    private JvmInfo jvmInfo;

    private JvmRuntimeInfo jvmRuntimeInfo;

    private LocalInfo localInfo;

    private MemoryInfo memoryInfo;

    private static SystemMonitor INSTANCE = new SystemMonitor();

    public SystemMonitor() {
        // 加载相关的信息
        cpuInfo = CpuUtils.readCpu();
        diskInfoList = DiskUtils.readDiskList();
        javaInfo = JvmUtils.readJavaInfo();
        jvmInfo = JvmUtils.readJvmInfo();
        jvmRuntimeInfo = JvmUtils.readJvmRuntime();
        localInfo = SystemUtils.readLocalInfo();
        memoryInfo = MemoryUtils.readMemory();
    }

    private JvmRuntimeInfo reloadJvmRuntime() {
        jvmRuntimeInfo = JvmUtils.readJvmRuntime();
        return jvmRuntimeInfo;
    }

    private CpuInfo reloadCpu() {
        cpuInfo = CpuUtils.readCpu();
        return cpuInfo;
    }

    private List<DiskInfo> reloadDisks() {
        DiskUtils.fillDisks(this.diskInfoList);
        return diskInfoList;
    }

    private MemoryInfo reloadMemory() {
        memoryInfo = MemoryUtils.readMemory();
        return memoryInfo;
    }

    public static CpuInfo cpu() {
        return INSTANCE.reloadCpu();
    }

    public static List<DiskInfo> disks() {
        return INSTANCE.reloadDisks();
    }

    public static JavaInfo java() {
        return INSTANCE.javaInfo;
    }

    public static JvmInfo jvm() {
        return INSTANCE.jvmInfo;
    }

    public static JvmRuntimeInfo jvmRuntime() {
        return INSTANCE.reloadJvmRuntime();
    }

    public static LocalInfo local() {
        return INSTANCE.localInfo;
    }

    public static MemoryInfo memory() {
        return INSTANCE.reloadMemory();
    }

}
